<template>
  <div>
    <div class="search">
      <el-input
        style="width: 200px; margin-right: 10px"
        size="small"
        v-model="form.title"
        placeholder="请输入标题"
      ></el-input>
      <el-button type="info" size="small" @click="chaxun()" plain
        >查询</el-button
      >
      <el-button type="warning" size="small" @click="chongzhi()" plain
        >重置</el-button
      >
      <el-button
        type="primary"
        size="small"
        style="float: right"
        @click="add()"
        plain
        >新增</el-button
      >
    </div>
    <div class="table">
      <el-table :data="tabledata">
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="内容" prop="content"></el-table-column>
        <el-table-column label="创建人" prop="cjr"></el-table-column>
        <el-table-column label="创建时间" prop="cjsj"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" plain @click="sh(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" @click="shanchu(scope.row)" plain
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total,prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form1">
        <el-form-item label="标题">
          <el-input v-model="form1.title"> </el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form1.content" type="textarea"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="shenheqd">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="封禁"
      :visible.sync="dialogVisible1"
      width="30%"
      :before-close="handleClose1"
    >
      <el-form :model="form1">
        <el-form-item label="封禁原因">
          <el-input
            v-model="form1.fjly"
            type="textarea"
            placeholder="请输入封禁原因"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="fjqd">确 定</el-button>
      </span></el-dialog
    >
  </div>
</template>
<script>
import service from '../request'

export default {
  data () {
    return {
      form: {
        title: '',
        pageNum: 1,
        pageSize: 10
      },
      title: '',
      form1: {},
      dialogVisible1: false,
      dialogVisible: false,

      total: 0,
      tabledata: []
    }
  },
  mounted () {
    this.getdata()
  },
  methods: {
        shanchu(row){
          this.$confirm('是否确认删除？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(async () => {

					const res = await service.get(`/index/shanchugg`,{
                        params:{
                            id:row.id
                        }
                    });
					if (res.code == 200) {
						this.$message({
							type: 'success',
							message: '删除成功!',
						});
						this.getdata();
					}
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除',
					});
				});
      },
    handleCurrentChange (e) {
      this.form.pageNum = e
      this.getdata()
    },
    handleSizeChange (e) {
      this.form.pageSize = e
      this.getdata()
    },
    fj (row) {
      this.form1 = JSON.parse(JSON.stringify(row))
      this.form1.fjly = ''
      this.form1.status = '1'
      this.dialogVisible1 = true
    },
    async fjqd () {
      const today = new Date()
      const year = today.getFullYear()
      const month = today.getMonth() + 1
      const day = today.getDate()
      let cjsj = year + '-' + month + '-' + day
      this.form1.cjsj = cjsj
      this.form1.cjr = JSON.parse(window.localStorage.getItem('user')).username
      const res = await service.post('/index/gonggaosave', this.form1)
      console.log(res)
      if (res.code == 200) {
        this.$message.success('修改成功')
        this.dialogVisible1 = false
        this.getdata()
      }
    },
    add () {
      this.form1 = {}
      this.title = '新增'
      this.dialogVisible = true
    },
    async shenheqd () {
         const today = new Date()
      const year = today.getFullYear()
      const month = today.getMonth() + 1
      const day = today.getDate()
      let cjsj = year + '-' + month + '-' + day
      this.form1.cjsj = cjsj
      this.form1.cjr = JSON.parse(window.localStorage.getItem('user')).username
      const res = await service.post('/index/gonggaosave', this.form1)
      console.log(res)
      if (res.code == 200) {
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.getdata()
      }
    },
    sh (row) {
      this.form1 = JSON.parse(JSON.stringify(row))


      this.dialogVisible = true
    },
    handleClose () {
      this.dialogVisible = false
    },
    handleClose1 () {
      this.dialogVisible1 = false
    },
    chongzhi () {
      this.form = {
        username: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getdata()
    },
    chaxun () {
      this.getdata()
    },
    async getdata () {
      const res = await service.get('/index/gonggao', {
        params: this.form
      })
      console.log(res)
      this.tabledata = res.data
      this.total = res.total
    }
  }
}
</script>
<style scoped>
</style>
